package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;

    @RequestMapping("/upload")
    //上传文件 提供MultipartFile excelFile文件名
    public Result upload(MultipartFile excelFile){
        //try
        try {
            //读取文件内容封装list集合
            List<String[]> list = POIUtils.readExcel(excelFile);
            //判断list集合是否为空
            if(list != null && list.size()>0) {
                //创新新的list集合用于存储预约信息
                List<OrderSetting> orderSettingList = new ArrayList<>();
                //遍历文件内容集合
                for (String[] strings : list) {
                    //获得第一列和第二列的数据
                    OrderSetting orderSetting = new OrderSetting(new Date(strings[0]),Integer.parseInt(strings[1]));
                    //将数据存入预约信息集合
                    orderSettingList.add(orderSetting);
                }
                //调用新增方法传入预约信息集合
                orderSettingService.add(orderSettingList);
            }
        }catch (Exception e){
            e.printStackTrace();
            //返回上传失败
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
        //返回上传成功
        return new Result(true,MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
    }

    @RequestMapping("/getOrderSettingByMonth")
    //根据日期获取预约信息用于页面展示
    public Result getOrderSettingByMonth(String date){
        //try
        try {
            //调用查询预约信息方法
            List<Map> list = orderSettingService.getOrderSettingByMonth(date);
            //返回查询成功
            return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            //返回查询失败
            return new Result(false,MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }
    @RequestMapping("/editNumberByDate")
    //编辑预约信息
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
        try {
            //调用编辑预约信息方法
            orderSettingService.editNumberByDate(orderSetting);
            //返回编辑成功
            return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            //返回编辑失败
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
    }
}
